/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mail;

import java.io.File;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author ren
 */
public class Mail implements Runnable{
    
    String destinatario;
    String testo;
    File allegato = null;
    String username;
    String password;
    String tipomail;
    String oggetto;
    
    public Mail(String dest, String test, String usr, String pass, String tipom, String ogg) {
    destinatario = dest;
    testo=test;
    username = usr;
    password = pass;
    tipomail = tipom;
    oggetto = ogg;
    
    }
    
     public Mail(String dest, String test, File a, String usr, String pass, String tipom, String ogg) {
    destinatario = dest;
    testo=test;
    allegato = a;
    username = usr;
    password = pass;
    tipomail = tipom;
    oggetto = ogg;
    }
    
     private Session getGmail() {

      
        
        
        

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");

        Session session = Session.getDefaultInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        return session;

    }

     
     
  private Session getUni() {
      
      Session session = null;
      
      Properties props = System.getProperties();
    props.put( "mail.smtp.host", "mail.unitn.it" );
    props.put( "mail.debug", "true" );
    session = Session.getDefaultInstance( props );
      
    return session;  
  }
     
     
     
    @Override
    public void run() {
        
        
        if (allegato == null) {
            
            
        try {
            
            Message msg = null;
            if (tipomail.equalsIgnoreCase("unitn")) {
                 msg = new MimeMessage(getUni());
                
            } else {
           msg = new MimeMessage(getGmail());
            
            }
            
               // — Set the FROM and TO fields –
               msg.setFrom(new InternetAddress("progetto.progweb@unitn.it"));
               msg.setRecipients(Message.RecipientType.TO,
                       InternetAddress.parse(destinatario, false));
               msg.setSubject(oggetto);
               msg.setText(testo);
               msg.setSentDate(new Date());
               Transport.send(msg);
        } catch (MessagingException ex) {
            Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        } else {
            
            //con allegato
                try {
            
                    
                     Message msg = null;
            if (tipomail.equalsIgnoreCase("unitn")) {
                 msg = new MimeMessage(getUni());
                
            } else {
           msg = new MimeMessage(getGmail());
            
            }
                    
               // — Set the FROM and TO fields –
               msg.setFrom(new InternetAddress("progetto.progweb@unitn.it"));
               msg.setRecipients(Message.RecipientType.TO,
                       InternetAddress.parse(destinatario, false));
               msg.setSubject(oggetto); 
               msg.setSentDate(new Date());
               
               Multipart multipart = new MimeMultipart();
               BodyPart bp1 = new MimeBodyPart();
               bp1.setText(testo);
               DataSource source = new FileDataSource(allegato);
                BodyPart bp2 = new MimeBodyPart();
                bp2.setDataHandler(new DataHandler(source));
                bp2.setFileName(allegato.getName());
                multipart.addBodyPart(bp1);
                multipart.addBodyPart(bp2);
                msg.setContent(multipart);
               Transport.send(msg);
               allegato.delete();
        } catch (MessagingException ex) {
            Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
        }
        
            
            
            
        }
        
    }
     
     
     
    
     
     
     
     
     
     
    
}
